package net.system.sys.service;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.core.common.DateFormater;
import net.core.common.StrUtils;
import net.system.base.bean.SSysnotice;
import net.system.base.bean.TAd;
import net.system.base.bean.TAdExample;
import net.system.sys.dao.TAdDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ADServiceImpl implements IADService {
	
	private static Log logger = LogFactory.getLog(ADServiceImpl.class);
	@Autowired
	private TAdDAO dao;
	@Autowired
	private ICommService commService;
	@Override
	public TAd getBean(String recId) {
		return dao.selectByPrimaryKey(recId);
	}

	@Override
	public List<TAd> getList() {
		TAdExample example = new TAdExample();
		TAdExample.Criteria c = example.createCriteria();
		List<TAd> list = dao.selectByExample(example);
		return list;
	}

	@Override
	public void insert(HashMap reqMap) {
		TAd record = new TAd();
		String recId = commService.getSeq("common");
		record.setRecId(recId);
		String adName = StrUtils.getNotNullStrValue(reqMap.get("adName"));
		record.setAdName(adName);
		String adContent = StrUtils.getNotNullStrValue(reqMap.get("adContent"));
		record.setAdContent(adContent);
		String adLink = StrUtils.getNotNullStrValue(reqMap.get("adLink"));
		record.setAdLink(adLink);
		String adPic = StrUtils.getNotNullStrValue(reqMap.get("adPic"));
		record.setAdPic(adPic);
		Date adStartdate= new Date();
		try {
			adStartdate = DateFormater.stringToDate(StrUtils.getNotNullStrValue(reqMap.get("adStartdate")));
		} catch (ParseException e) {
			logger.error("开始日期解析错误，内容:adStartdate="+adStartdate+"\n错误信息："+e.getMessage());
		}
		record.setAdStartdate(adStartdate);
		Date adEnddate = new Date();
		try {
			adEnddate = DateFormater.stringToDate(StrUtils.getNotNullStrValue(reqMap.get("adEnddate")));
		} catch (ParseException e) {
			logger.error("结束日期解析错误，内容:adEnddate="+adEnddate+"\n错误信息："+e.getMessage());
		}
		record.setAdEnddate(adEnddate);
		record.setEnabled(true);
		dao.insert(record);
	}

	@Override
	public void update(HashMap reqMap) {
		TAd record = new TAd();
		String recId = StrUtils.getNotNullStrValue(reqMap.get("recId"));
		record.setRecId(recId);
		String adName = StrUtils.getNotNullStrValue(reqMap.get("adName"));
		record.setAdName(adName);
		String adContent = StrUtils.getNotNullStrValue(reqMap.get("adContent"));
		record.setAdContent(adContent);
		String adLink = StrUtils.getNotNullStrValue(reqMap.get("adLink"));
		record.setAdLink(adLink);
		String adPic = StrUtils.getNotNullStrValue(reqMap.get("adPic"));
		record.setAdPic(adPic);
		Date adStartdate= new Date();
		try {
			adStartdate = DateFormater.stringToDate(StrUtils.getNotNullStrValue(reqMap.get("adStartdate")));
		} catch (ParseException e) {
			logger.error("开始日期解析错误，内容:adStartdate="+adStartdate+"\n错误信息："+e.getMessage());
		}
		record.setAdStartdate(adStartdate);
		Date adEnddate = new Date();
		try {
			adEnddate = DateFormater.stringToDate(StrUtils.getNotNullStrValue(reqMap.get("adEnddate")));
		} catch (ParseException e) {
			logger.error("结束日期解析错误，内容:adEnddate="+adEnddate+"\n错误信息："+e.getMessage());
		}
		record.setAdEnddate(adEnddate);
		record.setEnabled(true);
		dao.updateByPrimaryKeySelective(record);

	}

}
